<template>
  <view class="container">
		<view class="header">
			<view class="login">
				<view class="logo">
					<image v-if="userInfo.mobile" class="avatar-img" :src="userInfo.avatar" mode="aspectFill" @click="goSetPersonal"></image>
					<image v-else class="avatar-img" src="https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/pic/no_login.png" mode="aspectFill"></image>
				</view>
				<view class="user-name">
					<text class="name" @click="goWxLogin">{{userInfo.mobile ? userInfo.nickname : '点击登录'}}</text>
					<text class="tel">{{userInfo.mobile ? formatPhone(userInfo.username): ''}}</text>
				</view>
			</view>
			<view class="order-status">
				<view class="all-order" v-for="(item, index) in orderData" :key="index" @click="goOrderList(item, index)">
					<text v-if="userInfo.mobile" class="order-num">{{item.num}}</text>
					<text v-else class="order-num">***</text>
					<text class="order-title">{{item.name}}</text>
				</view>
			</view>
		</view>
		<view class="cu-list menu menu-list">
			<view class="cu-item arrow" v-for="(item, index) in menuList" :key="index" @click="goMySet(item)" v-if="item.show">
				<view class="content">
					<view class="content-left">
						<image :src="item.icon" class="img"></image>
						<text class="item-title">{{item.name}}</text>
					</view>
					<view class="content-right">
						<text class="item-title">{{item.remark}}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="other-applet">
			<view class="applet" v-for="(item, index) in appletData" :key="index" @click="goApplet(item)">
				<image :src="item.img" class="applet-img"></image>
				<text class="applet-text">{{item.name}}</text>
			</view>
		</view>
  </view>
</template>

<script>
	
import homeApi from "@/api/home.js"
import personalApi from "@/api/personal.js"
export default {
  data() {
    return {
			userInfo: {},
			orderObj: {},
			orderData: [
				{
					id: '',
					name: '全部订单',
					num: null,
					index: 0
				},
				{
					id: 0,
					name: '待付款',
					num: null,
					index: 1
				},
				{
					id: 1,
					name: '服务中',
					num: null,
					index: 2
				},
				{
					id: 4,
					name: '已完成',
					num: null,
					index: 4
				}
			],
			menuList: [
				{
					icon: 'https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/enterprise-service/shopImg/myShop-icon.png',
					name: '我的商品',
					remark: '',
					url: '/pagesShop/myShop/myShop',
					show: false
				},
				{
					icon: 'https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/enterprise-service/personal/myDemand.png',
					name: '我的需求',
					remark: '',
					url: '/pagesDemand/myDemand/myDemand',
					show: true
				},
				{
					icon: 'https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/coupon/coupon_icon_personal.png',
					name: '我的代金券',
					remark: '',
					url: '/pagesPersonal/myCoupons/myCoupons',
					handler: '',
					show: true
				},
				{
					icon: 'https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/New-my/icon-sc.png',
					name: '我的收藏',
					remark: '',
					url: '/pagesPersonal/myCollect/myCollect',
					show: true
				},
				{
					icon: 'https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/New-my/myNewsIcon.png',
					name: '我的消息',
					remark: '',
					url: '/pagesMy3/myNews/myNews',
					show: true
				},
				{
					icon: 'https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/New-my/icon-sz.png',
					name: '设置',
					remark: '',
					url: '/pagesOrder/mySetting/mySetting',
					show: true
				},
				{
					icon: 'https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/enterprise-service/personal/myInvite.png',
					name: '我的邀请',
					remark: '',
					url: '/pagesPersonal/myInvite/myInvite',
					show: true
				},
				{
					icon: 'https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/New-my/icon-lxwm.png',
					name: '联系客服',
					remark: '',
					url: '',
					handler: 'contactHandler',
					show: true
				},
			],
			
			appletData: [
				{
					img: 'https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/enterprise-service/personal/wentiku.png',
					name: '小竹财税',
					appId: 'wx99f5b742cdba884d'
				},
				{
					img: 'https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/enterprise-service/personal/xzwd.png',
					name: '小竹问答',
					appId: 'wxb012d8e690b5eca3'
				},
				{
					img: 'https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/enterprise-service/personal/fgk.png',
					name: '小竹法规库',
					appId: 'wxb485d65cd012f6de'
				},
				{
					img: 'https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/enterprise-service/personal/xzxy.png',
					name: '小竹学院',
					appId: 'wxaa1695f9b1b1a087'
				}
			]
		};
  },
	onShow() {
		if(uni.getStorageSync('token')) {
			this.getUserInfo()
			this.getOrderStatus()
		}else{
			this.userInfo = {}
		}
	},
	methods: {
		//手机号码保密
		formatPhone(phone) {
			return phone.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
		},
		
		//获取用户信息
		getUserInfo() {
			homeApi.getUserInfo().then(res => {
				if(res.code === 1) {
					uni.setStorageSync('userInfo', res.data)
					this.userInfo = res.data
					if(res.data.is_server == 1) {
						this.menuList[0].show = true
					}else {
						this.menuList[0].show = false
					}
				}else {
					this.$common.msgShow(res.msg)
				}
			})
		},
		
		//获取订单状态数据
		getOrderStatus() {
			personalApi.orderStatus().then(res => {
				if(res.code === 1) {
					this.orderData[0].num = res.data.shopordercount.allcount
					this.orderData[1].num = res.data.shopordercount.status0
					this.orderData[2].num = res.data.shopordercount.status1
					this.orderData[3].num = res.data.shopordercount.status3 + res.data.shopordercount.status4
					this.menuList[0].remark = res.data.shop_count
					this.menuList[1].remark = res.data.demand_count
				}else {
					this.$common.msgShow(res.msg)
				}
			})
		},
		
		//跳转我的订单页面
		goOrderList(item, index) {
			uni.navigateTo({
				url: '/pagesOrder/myOrder/myOrder?index=' + item.index
			})
		},
		
		//前往登录页
		goWxLogin() {
			if(!uni.getStorageSync('token')) {
				uni.navigateTo({
					url: '/pagesLogin/wxLogin/wxLogin'
				})
			}else {
				return
			}
		},
		
		//跳转设置页面
		goMySet(item) {
			if (!uni.getStorageSync("token")) {
				this.$common.noToken()
				return
			}
			if (item.handler) this[item.handler]()
			else if (item.url) this.$common.href(item.url)
		},
		
		contactHandler() {
			wx.openCustomerServiceChat({
			  extInfo: {url: 'https://work.weixin.qq.com/kfid/kfc8ba259d66968b838'},
			  corpId: 'ww447880e03d7eba27',
			  success(res) {}
			})
		},
		
		
		//跳转个人信息
		goSetPersonal() {
			uni.navigateTo({
				url: '/pagesOrder/setPersonal/setPersonal'
			})
		},
		
		//跳转小程序
		goApplet(item) {
			wx.navigateToMiniProgram({
				appId: item.appId, // 需要跳转到指定的小程序appid
				path: 'pages/index/index',   // 打开的页面
				envVersion: 'release',
				success(res) {
					// 打开成功
					console.log(res);
				}
			})
		}
	}
}
</script>

<style lang="scss" scoped>
	@import "personal.scss";
</style>
